package lib.Dao;

import lib.Model.Course;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;


public class CourseUserDAO {
    public List<String> getCourseUser(String cid) throws SQLException{
        Statement stmt = null;
        Dbutil dbutil = new Dbutil();
        Connection con = null;
        ResultSet rs = null;
        List<String> list =new ArrayList<String>();

         try{
            con = dbutil.getCon();
            stmt = con.createStatement();
            String sql = "select * from "+cid;
            rs = stmt.executeQuery(sql);
            while(rs.next()) {
              String openid=rs.getString("openid");
              list.add(openid);
            }
            return list;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public void CourseUseradd(String cid,String openid) throws SQLException{
        Statement stmt = null;
        Dbutil dbutil = new Dbutil();
        Connection con = null;
        ResultSet rs = null;
        List<Course> list =new ArrayList<>();

        try{
            con = dbutil.getCon();
            PreparedStatement pst = null;
            //System.out.println(content);
            stmt = con.createStatement();
            // INSERT INTO Student(sId,sName) VALUES (?,?)  "delete from notes where notes_id = ?";


            String sql1="CREATE TABLE IF NOT EXISTS "+cid+"(openid VARCHAR(100) NOT NULL,PRIMARY KEY ( openid ))ENGINE=InnoDB DEFAULT CHARSET=utf8;";
            stmt.executeUpdate(sql1);


            String sql2= "insert into "+cid+" (openid) values ('"+openid+"')";
            stmt.executeUpdate(sql2);

            return ;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return ;
    }


    public void CourseUserDelete(String cid,String openid) throws SQLException{
        Statement stmt = null;
        Dbutil dbutil = new Dbutil();
        Connection con = null;
        ResultSet rs = null;
        List<Course> list =new ArrayList<>();

        try{
            con = dbutil.getCon();
            PreparedStatement pst = null;
            //System.out.println(content);
            stmt = con.createStatement();
            // INSERT INTO Student(sId,sName) VALUES (?,?)  "delete from notes where notes_id = ?";


            String sql2= "delete from "+cid+" where openid like '"+openid+"'";
            stmt.executeUpdate(sql2);

            return ;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return ;
    }


}
